#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>

#define CSIZE 32


int main(int argc,char* const argv){

    char buf[CSIZE]="";
    int fd[2]={0};
    if(pipe(fd) < 0){
        perror("pipe err");
        return -1;
    }

    pid_t pid=-1;
    pid = fork();
    if(pid < 0){
        perror("fork err");
        return -1;
    }else if(pid == 0){
         while(1){
            if(read(fd[0],buf,CSIZE) == -1){
                break;
            }
            
            if(strcmp(buf,"quit") == 0){
                break;
            }
            printf("子：buf:%s\n",buf);
        }
        close(fd[0]);
        close(fd[1]);
        

    }else{
        while(1){
            scanf("%s",buf);
            if(write(fd[1],buf,CSIZE) == -1){
                strcpy(buf,"quit");
                break; 
            }
            
            if(strcmp(buf,"quit") == 0){
                break;
            }
            
        }
        wait(NULL); 
        close(fd[0]);
        close(fd[1]);
    }
    

    return 0;
}